package com.tianji.promotion.listener;

import com.tianji.api.dto.promotion.CheckIssueStartMsgDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.JsonUtils;
import com.tianji.promotion.service.ICouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CheckIssueStartListener {

    @Autowired
    private ICouponService couponService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.Queue.PROMOTION_DELAY_QUEUE),
            exchange = @Exchange(name = MqConstants.Exchange.PROMOTION_DELAY_EXCHANGE,
                    delayed = "true",
                    type = ExchangeTypes.DIRECT),
            key = MqConstants.Key.PROMOTION_DELAY_KEY
    ))
    public void onCheckIssueStart(CheckIssueStartMsgDTO dto){
        log.info("收到优惠券开始的消息：{}", JsonUtils.toJsonStr(dto));
        couponService.startIssue(dto);
    }
}
